<?php

namespace app\app\controller;

use app\app\model\ProjectStypModel;
use app\BaseController;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use function input;

class ProjectFourConnectionOneLevel extends BaseController
{
    /**
     * 四通一平列表
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $project_id = input('project_id', '');
        $page_size = input('pageSize', '20');

        if (empty($project_id)) $this->error('请传入project_id');

        $list = Db::table('t_project_styp')
            ->where('project_id', $project_id)
            ->paginate($page_size);

        if (empty($list)) $this->error('查询为空');

        $this->success('success', $list);
    }

    /**
     * 查询四通一平详情
     * @return void
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function detail()
    {
        $data = $this->request->param();
        if (!$data['project_id']) $this->error('请传入id');

        $data = ProjectStypModel::where('project_id', $data['project_id'])->find();

        if (empty($data)) $this->error('不存在该数据');
        $this->success('success', $data);
    }

    /** 添加或修改四通一平
     * @return void
     */
    public function update()
    {
        $data = $this->request->param();
        if (!$data['project_id']) $this->error('请传入id');

        $info = ProjectStypModel::where('project_id', $data['project_id'])->find();
        if (empty($info)){
            $res = ProjectStypModel::create($data);
        }else{
            $res = $info->save($data);
        }
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
}
